﻿using System;
using System.Data;
using System.Data.Common;
using System.Reflection;
using System.Windows.Forms;

namespace YJKRevitFamilyDataBase
{
	public class SqliteElectricDatabase : Database
	{
		public string Name
		{
			get
			{
				return this.mName;
			}
		}

		public override string ConnStr
		{
			get
			{
				return "Data Source=" + this.mName + ";Initial Catalog=sqlite;Integrated Security=True;Max Pool Size=10";
			}
		}

		public SqliteElectricDatabase(string fileName)
		{
			this.mName = fileName;
			object obj = Assembly.LoadFrom(SqliteDatabase.SqlitePath).CreateInstance(SqliteDatabase.SQLiteConnection, true, BindingFlags.CreateInstance, null, new object[]
			{
				this.ConnStr
			}, null, new object[0]);
			obj.GetType().GetMethod("Open").Invoke(obj, new object[0]);
			this.Connection = (obj as DbConnection);
		}

		public DataTable GetAllLampType()
		{
			return base.ExecQuery(string.Format("select [LampType] from [LampType]", Array.Empty<object>()));
		}

		public DataTable GetAllFactory()
		{
			return base.ExecQuery(string.Format("select [LampFactory] from [Illuminant]  group by [LampFactory]", Array.Empty<object>()));
		}

		public void FillIllAndPowerTree(TreeView tv)
		{
			tv.Nodes.Clear();
			DataTable dataTable = base.ExecQuery(string.Format("select [Trade] from [IllAndPower]  group by [Trade]", Array.Empty<object>()));
			for (int i = 0; i < dataTable.Rows.Count; i++)
			{
				TreeNode treeNode = new TreeNode(dataTable.Rows[i][0].ToString());
				DataTable dataTable2 = base.ExecQuery(string.Format("select [BuildingType] from [IllAndPower] where [Trade]='{0}' group by [BuildingType]", dataTable.Rows[i][0].ToString()));
				for (int j = 0; j < dataTable2.Rows.Count; j++)
				{
					TreeNode node = new TreeNode(dataTable2.Rows[j][0].ToString());
					treeNode.Nodes.Add(node);
				}
				tv.Nodes.Add(treeNode);
			}
		}

		protected string mName = "";
	}
}
